package com.wsd.dao.impl;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsd.bean.Articles;
import com.wsd.dao.ArticlesDaoInterface;

/**
 * A data access object (DAO) providing persistence and search support for
 * Articles entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wsd.bean.Articles
 * @author MyEclipse Persistence Tools
 */

public class ArticlesDAO extends HibernateDaoSupport implements ArticlesDaoInterface {
	private static final Log log = LogFactory.getLog(ArticlesDAO.class);

	protected void initDao() {
		// do nothing
	}

	public void save(Articles transientInstance) {
		log.debug("saving Articles instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Articles persistentInstance) {
		log.debug("deleting Articles instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Articles findById(java.lang.Integer id) {
		log.debug("getting Articles instance with id: " + id);
		try {
			Articles instance = (Articles) getHibernateTemplate().get(
					"com.wsd.bean.Articles", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Articles> findAll() {
		log.debug("finding all Articles instances");
		try {
			String queryString = "from Articles";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#history(java.util.Date, java.util.Date, int, int)
	 */
	public List<Articles> history(Date startDate, Date endDate, int start, int limit) {
		String sql = "from Articles where postTime between :startDate and :endDate order by AId desc";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setDate("startDate", startDate);
		query.setDate("endDate", endDate);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		return query.list();
	}

	public int historyCount(Date startDate, Date endDate) {
		String sql = "select count(*) from Articles where postTime between :startDate and :endDate";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setDate("startDate", startDate);
		query.setDate("endDate", endDate);
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#delete(int)
	 */
	public void delete(int id) {
		String sql = "delete Articles where AId = :id";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setInteger("id", id);
		query.executeUpdate();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#update(com.wsd.bean.Articles)
	 */
	public void update(Articles preArticle) {
		getHibernateTemplate().save(preArticle);
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#findAll(int, int)
	 */
	public List<Articles> findAll(int start, int limit) {
		String sql = "from Articles";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setMaxResults(limit);
		query.setFirstResult(start);
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#findAll(com.wsd.bean.Articles, int, int)
	 */
	public List<Articles> findAll(Articles article, int start, int limit) {
		return getHibernateTemplate().findByExample(article, start, limit);
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#findAll(com.wsd.bean.Articles)
	 */
	public int findAllCount(Articles article) {
		String sql = "select count(*) from Articles where AModel = :model";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setInteger("model", article.getAModel());
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#findAllCount()
	 */
	public int findAllCount() {
		String sql = "select count(*) from Articles";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#findByModel(int, int, int)
	 */
	@SuppressWarnings("unchecked")
	public List<Articles> findByModel(int start, int pagesize, int model) {
		log.debug("get Articles totallines by model");
		String queryString = "from Articles where AModel = ? order by postTime desc";
		try {
			Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
			query.setInteger(0, model);
			query.setFirstResult(start);
			query.setMaxResults(pagesize);
			return query.list();
		} catch (RuntimeException re) {
			log.error("get Articles totallines  by model fail", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ArticlesDaoInterface#getTotalLines(int, int)
	 */
	public int getTotalLines(int model) {
		log.debug("get Articles totallines by model");
		String queryString = "select count(*) from Articles where AModel = ?";
		try {
			Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
			query.setInteger(0, model);
			return ((Long) query.uniqueResult()).intValue();
		} catch (RuntimeException re) {
			log.error("get Articles totallines  by model fail", re);
			throw re;
		}
	}
}